﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebLibs;
using System.Data;
using MDF.DataHelper.BussinessObject.NewsBO;

namespace MDF.DataHelper.DataObject.NewsDO
{
    public class NewsDAO : BaseDataObject
    {
        #region Constructor

        public NewsDAO()
        {
        }

        public NewsDAO(IData _objData)
        {
            this.m_objData = _objData;
        }
        #endregion

        #region Methods

        public DataTable GetAll(string _strSearch,int _iLang,int _iTypeID, int _iPage, int _iPageSize)
        {
            try
            {
                this.Connect();

                this.m_objData.CreateNewStoredProcedure("[News_GetAll]");
                this.m_objData.AddParameter("@Search", _strSearch);
                this.m_objData.AddParameter("@TypeID", _iTypeID);
                this.m_objData.AddParameter("@LangID", _iLang);
                this.m_objData.AddParameter("@Page", _iPage);
                this.m_objData.AddParameter("@PageSize", _iPageSize);
                return this.m_objData.ExecStoreToDataTable();

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }

        public DataTable GetOlderNews(int _iLang, int _iNewsID, int _iAmount)
        {
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[News_GetOlderNews]");
                this.m_objData.AddParameter("@LangID", _iLang);
                this.m_objData.AddParameter("@NewsID", _iNewsID);
                this.m_objData.AddParameter("@Amount", _iAmount);
                return this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }

        public DataTable GetlatestPinedNews(int _iLang, int _iAmount)
        {
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[News_GetlatestPinedNews]");
                this.m_objData.AddParameter("@LangID", _iLang);
                this.m_objData.AddParameter("@Amount", _iAmount);
                return this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }

        public void Delete(int _iNewsID, string _strUsername)
        {
            try
            {
                this.Connect();

                this.m_objData.CreateNewStoredProcedure("[News_Delete]");
                this.m_objData.AddParameter("@NewsID", _iNewsID);
                this.m_objData.AddParameter("@Username", _strUsername);

                m_objData.ExecNonQuery();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }

        public DataTable GetByType(int _iTypeNews, int _iLang, int _iPage, int _iPageSize)
        {
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[News_GetAll]");
                this.m_objData.AddParameter("@TypeNews", _iTypeNews);
                this.m_objData.AddParameter("@LangID", _iLang);
                this.m_objData.AddParameter("@Page", _iPage);
                this.m_objData.AddParameter("@PageSize", _iPageSize);
                return this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }

        public int Insert(int ID, int LangID
            , int TypeID, string Title, string Link
            , string Description, string Content, string Image
            , string RefLink, string SEO, string CreatedUser
            , string IP, bool IsPin)
        {
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[News_Insert]");
                this.m_objData.AddParameter("@ID", ID);
                this.m_objData.AddParameter("@LangID", LangID);
                this.m_objData.AddParameter("@TypeID", TypeID);
                this.m_objData.AddParameter("@Title", Title);
                this.m_objData.AddParameter("@Link", Link);
                this.m_objData.AddParameter("@Description", Description);
                this.m_objData.AddParameter("@Content", Content);
                this.m_objData.AddParameter("@Image", Image);
                this.m_objData.AddParameter("@RefLink", RefLink);
                this.m_objData.AddParameter("@SEO", SEO);
                this.m_objData.AddParameter("@CreatedUser", CreatedUser);
                this.m_objData.AddParameter("@IP", IP);
                this.m_objData.AddParameter("@IsPin", IsPin);
                DataTable td = this.m_objData.ExecStoreToDataTable();
                if (td == null || td.Rows.Count == 0)
                    return -1;
                return Int32.Parse(td.Rows[0][0].ToString());
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }

        public bool Update(int ID, int LangID
            , int TypeID, string Title, string Link
            , string Description, string Content, string Image
            , string RefLink, string SEO, string UpdatedUser
            , string IP, bool IsPin)
        {
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[News_Update]");
                this.m_objData.AddParameter("@ID", ID);
                this.m_objData.AddParameter("@LangID", LangID);
                this.m_objData.AddParameter("@TypeID", TypeID);
                this.m_objData.AddParameter("@Title", Title);
                this.m_objData.AddParameter("@Link", Link);
                this.m_objData.AddParameter("@Description", Description);
                this.m_objData.AddParameter("@Content", Content);
                this.m_objData.AddParameter("@Image", Image);
                this.m_objData.AddParameter("@RefLink", RefLink);
                this.m_objData.AddParameter("@SEO", SEO);
                this.m_objData.AddParameter("@UpdatedUser", UpdatedUser);
                this.m_objData.AddParameter("@IP", IP);
                this.m_objData.AddParameter("@IsPin", IsPin);
                this.m_objData.ExecNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }

        #endregion

        public DataTable GetByID(int _iNewsID, int _iLang)
        {
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[News_GetByID]");
                this.m_objData.AddParameter("@LangID", _iLang);
                this.m_objData.AddParameter("@NewsID", _iNewsID);
                return this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }

        public DataTable GetlatestNewsLimitDate(string _strSearch, int _iLang, int _iTypeID, int _iPage, int _iPageSize)
        {
            try
            {
                this.Connect();

                this.m_objData.CreateNewStoredProcedure("[News_GetlatestNewsLimitDate]");
                this.m_objData.AddParameter("@Search", _strSearch);
                this.m_objData.AddParameter("@TypeID", _iTypeID);
                this.m_objData.AddParameter("@LangID", _iLang);
                this.m_objData.AddParameter("@Page", _iPage);
                this.m_objData.AddParameter("@PageSize", _iPageSize);
                return this.m_objData.ExecStoreToDataTable();

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }
    }
}
